/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package plan.mobile;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import mobile.Call;
import mobile.Message;
import mobile.MobileConsumption;
import plan.Plan;
import turnover.Consumption;

/**
 *
 * @author Alberto
 */
public class Orange_tp_24h extends Plan {

    private double startQuote = 15d;
    private double minuteCost = 0.18d;
    private double messageCost = 0.15d;
    /*Tarifa Plana 24h es compatible con todos los módulos de ahorro
     (a excepción de Fiijos 24h) e incompatible con todas las
     promociones Orange (a excepción de Amigos a 0 y Domingos Orange).*/
    public Orange_tp_24h() {
        super.setName("Orange Tarifa Plana 24 Horas");
    }

    public double getStartQuote() {
        return startQuote;
    }

    @Override
    public double getCost(Consumption consumption) {
        double total = 0d;
        MobileConsumption mc = (MobileConsumption) consumption;
        List<Long> numList = new LinkedList();
        long durationMaxMobile = 500 * 60;
        long durationMax = 1000 * 60;
        long duration = 0;
        long durationMobile = 0;
        Iterator it = mc.getCallList().listIterator();
        while (it.hasNext()) {
            Call c = (Call) it.next();


            if (c.isSpecial()) {
                total += c.getCost();
            } else if (!(this.more75Numbers(numList, c) && this.overDurationMax(durationMax, duration) && this.overDurationMaxMobile(durationMaxMobile, durationMobile) && this.overOneHour(c)) && c.isMobile()) {
                duration += c.getDuration().changeSeconds();
                durationMobile += c.getDuration().changeSeconds();
                if (!numList.contains(c.getMobile())) {
                    numList.add(c.getMobile());
                }
            } else if (!(this.more75Numbers(numList, c) && this.overDurationMax(durationMax, duration) && this.overDurationMaxMobile(durationMaxMobile, durationMobile)) && this.overOneHour(c) && c.isMobile()) {
                total += c.getDuration().changeSeconds() - 3600 * this.minuteCost / 60;
                duration += 3600;
                durationMobile += 3600;
                if (!numList.contains(c.getMobile())) {
                    numList.add(c.getMobile());
                }
            } else if (!(this.more75Numbers(numList, c) && this.overDurationMax(durationMax, duration) && c.isMobile() && this.overOneHour(c))) {//no es movil y cumple las condiciones
                duration += c.getDuration().changeSeconds();
                if (!numList.contains(c.getMobile())) {
                    numList.add(c.getMobile());
                }
            } else if (!(this.more75Numbers(numList, c) && this.overDurationMax(durationMax, duration) && c.isMobile()) && this.overOneHour(c)) {//no es movil, supera la hora y cumple las condiciones
                total += c.getDuration().changeSeconds() - 3600 * this.minuteCost / 60;
                duration += 3600;
                if (!numList.contains(c.getMobile())) {
                    numList.add(c.getMobile());
                }
            } else {
                total += c.getDuration().changeSeconds() * this.minuteCost / 60;
            }
        }
        it = mc.getMessList().listIterator();
        while (it.hasNext()) {
            Message m = (Message) it.next();
            if (m.isSpecial()) {
                total += m.getCost();
            } else {
                total += this.messageCost;
            }
        }

        if (total<59d){
            total = 59d;
        }

        total += total * 0.16d;
        return total;
    }

    private boolean more75Numbers(List<Long> numList, Call c) {
        return (!numList.contains(c.getMobile()) && numList.size() == 75);
    }

    private boolean overOneHour(Call c) {
        return c.getDuration().changeSeconds() > 3600;
    }

    private boolean overDurationMax(long durationMax, long duration) {
        return durationMax <= duration;
    }

    private boolean overDurationMaxMobile(long durationMaxMobile, long durationMobile) {
        return durationMaxMobile <= durationMobile;
    }

    public double getMessageCost() {
        return messageCost;
    }

    public double getMinuteCost() {
        return minuteCost;
    }
}


